package com.wumart.scaffold.web.base.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.wumart.scaffold.web.base.datasource.property.G6DuridProperty;
import lombok.Data;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @author wangwc@wumart.com
 * @version v1.0.0
 * @date 2021/1/19 18:39
 */
@Data
@Configuration
@MapperScan(basePackages = G6DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "g6SqlSessionFactory")
public class G6DataSourceConfig extends MyBatisCommonConfig{

    // 精确到 g6 目录，以便跟其他数据源隔离
    static final String PACKAGE = "com.wumart.scaffold.web.mapper.g6";
    static final String MAPPER_LOCATION = "classpath:mapper/g6/*.xml";
    static final String HANDLER_PACKAGE = "com.wumart.scaffold.web.mapper.handler";

    @Autowired
    private G6DuridProperty g6DuridProperty;

    @Bean(name = "g6DataSource")
    public DataSource g6DataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(g6DuridProperty.getDriverClassName());
        dataSource.setUrl(g6DuridProperty.getUrl());
        dataSource.setUsername(g6DuridProperty.getUsername());
        dataSource.setPassword(g6DuridProperty.getPassword());
        try {
            dataSource.setFilters(g6DuridProperty.getFilters());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        dataSource.setMinIdle(g6DuridProperty.getMinIdle());
        dataSource.setInitialSize(g6DuridProperty.getInitialSize());
        dataSource.setMaxActive(g6DuridProperty.getMaxActive());
        dataSource.setMaxWait(g6DuridProperty.getMaxWait());
        dataSource.setTimeBetweenLogStatsMillis(g6DuridProperty.getTimeBetweenEvictionRunsMillis());
        dataSource.setMinEvictableIdleTimeMillis(g6DuridProperty.getMinEvictableIdleTimeMillis());
        dataSource.setValidationQuery(g6DuridProperty.getValidationQuery());
        dataSource.setTestWhileIdle(g6DuridProperty.getTestWhileIdle());
        dataSource.setTestOnBorrow(g6DuridProperty.getTestOnBorrow());
        dataSource.setTestOnReturn(g6DuridProperty.getTestOnBorrow());
        dataSource.setPoolPreparedStatements(g6DuridProperty.getPoolPreparedStatements());
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(g6DuridProperty.getMaxPoolPreparedStatementPerConnectionSize());
        return dataSource;
    }

    @Bean(name = "g6TransactionManager")
    public DataSourceTransactionManager g6TransactionManager(@Qualifier("g6DataSource") DataSource g6DataSource) {
        return new DataSourceTransactionManager(g6DataSource);
    }

    @Bean(name = "g6SqlSessionFactory")
    public SqlSessionFactory g6SqlSessionFactory(@Qualifier("g6DataSource") DataSource g6DataSource
                                                 )
            throws Exception {
        final MybatisSqlSessionFactoryBean sessionFactory = mybatisSqlSessionFactoryBeanBuilder();
        sessionFactory.setDataSource(g6DataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(G6DataSourceConfig.MAPPER_LOCATION));
        sessionFactory.setTypeHandlersPackage(HANDLER_PACKAGE);
        return sessionFactory.getObject();
    }
}
